package yxy.game.pm2.active.module;

import org.joda.time.DateTime;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.bean.actweekdiscount;
import yxy.game.pm2.active.ActiveUtil;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.active.module.lockorder.LockOrderInfo;
import yxy.game.pm2.active.module.subordinateclass.DataIdOrderId;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.AreaModule;
import yxy.game.pm2.bean.module.PlayerModule;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 以自然日周一至周七为一轮  活动控制模块不控制  结束时间为轮次结束时间 
 * @author Dell
 *
 */
public class ActiveModule_每周特惠 extends PlayerModule {

	//bean --------------------------
	
	public static final int acttype = 104;
	
	private DateTime updatetime;		//更新时间
	private ArrayList<DataIdOrderId> finish;		//dataid - info						
	private ArrayList<DataIdOrderId> lock;			//dataid - info	 info:orderid - time,	
	
	private DateTime roundend;		//本轮结束时间
							
	public DateTime getUpdatetime() {
		return updatetime;
	}
	public void setUpdatetime(DateTime updatetime) {
		this.updatetime = updatetime;
	}
	public ArrayList<DataIdOrderId> getFinish() {
		return finish;
	}
	public void setFinish(ArrayList<DataIdOrderId> finish) {
		this.finish = finish;
	}
	public ArrayList<DataIdOrderId> getLock() {
		return lock;
	}
	public void setLock(ArrayList<DataIdOrderId> lock) {
		this.lock = lock;
	}
	public DateTime getRoundend() {
		return roundend;
	}
	public void setRoundend(DateTime roundend) {
		this.roundend = roundend;
	}
	
	//fun --------------------------------------------------
	
	static public ActiveModule_每周特惠 create(String playerCode, int areaid) {
		
		ActiveModule_每周特惠 am = new ActiveModule_每周特惠();
		
		DateTime now = DateTime.now();
		DateTime weekend = ActiveUtil.getWeekEnd(now);
		am.setRoundend(weekend);
		
		am.setPlayerCode(playerCode);
		am.setUpdatetime(now);
		am.setFinish(new ArrayList<DataIdOrderId>());
		am.setLock(new ArrayList<DataIdOrderId>());
		am.set();
		return am;
	}

	/**
	 * 锁订单	
	 * @param dataid
	 * @param myorderid
	 */
	public void locknumnfdataid(int dataid, String myorderid) {
		ArrayList<DataIdOrderId> locklist = getLock();
		
		boolean have = false;
		for(int i = 0; i < locklist.size(); i++) {
			DataIdOrderId dio = locklist.get(i);
			if(dio.getDataid() == dataid) {
				HashMap<String, DateTime> odmap = dio.getMyorderinfoMap();
				
				if(odmap == null) {
					odmap = new HashMap<String, DateTime>();
				}
				
				if(!odmap.containsKey(myorderid)) {
					odmap.put(myorderid, DateTime.now());
				}
				
				dio.setMyorderinfoMap(odmap);
				have = true;
			}
		}
		
		if(!have) {
			DataIdOrderId dio = new DataIdOrderId();
			dio.setDataid(dataid);
			HashMap<String, DateTime> odmap = new HashMap<String, DateTime>();
			odmap.put(myorderid, DateTime.now());
			dio.setMyorderinfoMap(odmap);
			locklist.add(dio);
		}	
		
//		setUpdatetime(DateTime.now());
		set();	
	}
	
	/**
	 * 锁单到完成单
	 * @param dataid
	 * @param myorderid
	 */
	public void locktofinish(int dataid, String myorderid) {
		ArrayList<DataIdOrderId> locks = getLock();
		
		for(int i = 0; i < locks.size(); i++) {
			DataIdOrderId lockdio = locks.get(i);
			if(lockdio.getDataid() == dataid) {
				HashMap<String, DateTime> odmap = lockdio.getMyorderinfoMap();
				if(odmap == null) {
					continue;
				}
				odmap.remove(myorderid);
				lockdio.setMyorderinfoMap(odmap);
			}
		}	
		
		
		ArrayList<DataIdOrderId> finishs = getFinish();
		boolean have = false;
		for(int i = 0; i < finishs.size(); i++) {
			DataIdOrderId finishdio = finishs.get(i);
			if(finishdio.getDataid() == dataid) {
				HashMap<String, DateTime> odmap = finishdio.getMyorderinfoMap();
				if(odmap == null) {
					odmap = new HashMap<String, DateTime>();
				}
				odmap.put(myorderid, DateTime.now());
				finishdio.setMyorderinfoMap(odmap);
				have = true;
			}
		}
		
		if(!have) {
			DataIdOrderId dio = new DataIdOrderId();
			dio.setDataid(dataid);
			HashMap<String, DateTime> odmap = new HashMap<String, DateTime>();
			odmap.put(myorderid, DateTime.now());
			dio.setMyorderinfoMap(odmap);
			finishs.add(dio);
		}
		
		set();
	}
	
	/**
	 * 解除锁单
	 * @param dataid
	 * @param myorderid
	 */
	public void removelock(int dataid, String myorderid) {
		ArrayList<DataIdOrderId> locklist = getLock();
		
		for(int i = 0; i < locklist.size(); i++) {
			DataIdOrderId dio = locklist.get(i);
			if(dio.getDataid() == dataid) {
				HashMap<String, DateTime> odmap = dio.getMyorderinfoMap();
				if(odmap == null) {
					continue;
				}
				odmap.remove(myorderid);
			}
		}
		
		set();
	}
	
	
	/**
	 * 能否购买
	 * @param dataid
	 * @return
	 */
	public boolean canbuy(int dataid) {
		int finish = 0;
		ArrayList<DataIdOrderId> finishs = getFinish();
		for(int i = 0; i < finishs.size(); i++) {
			DataIdOrderId finishdio = finishs.get(i);
			if(finishdio.getDataid() == dataid) {
				HashMap<String, DateTime> odmap = finishdio.getMyorderinfoMap();
				if(odmap == null) {
					continue;
				}
				finish = odmap.size();
			}
		}
		
		int lock = 0;
		ArrayList<DataIdOrderId> locks = getLock();
		for (int i = 0; i < locks.size(); i++) {
			DataIdOrderId lockdio = locks.get(i);
			if(lockdio.getDataid() == dataid) {
				HashMap<String, DateTime> odmap = lockdio.getMyorderinfoMap();
				if(odmap == null) {
					continue;
				}
				lock = odmap.size();
			}
		}

		actweekdiscount awc = ActDataCenter.getData(dataid, actweekdiscount.class, getPlayerCode());
		int maxlimit = awc.getNum();
		
		if(finish + lock < maxlimit) {
			return true;
		}
		
		return false;
	}
	
	private void update() {
		DateTime now = DateTime.now();
		
		if(now.isAfter(getRoundend())) {
			//转周刷新
			DateTime weekend = ActiveUtil.getWeekEnd(now);
			setRoundend(weekend);

			setUpdatetime(now);
			setFinish(new ArrayList<DataIdOrderId>());
			setLock(new ArrayList<DataIdOrderId>());
			
			set();
		}
		
		//检查锁单
		boolean haveunlock = false;
		ArrayList<DataIdOrderId> locks = getLock();
		for (int i = 0; i < locks.size(); i++) {
			DataIdOrderId lockdio = locks.get(i);

			HashMap<String, DateTime> odmap = lockdio.getMyorderinfoMap();
			if (odmap == null) {
				continue;
			}

			// 检查锁单时间
			ArrayList<String> orderids = new ArrayList<String>();
			for (String orderid : odmap.keySet()) {
				DateTime time = odmap.get(orderid);
				if (DateTime.now().getMillis() - time.getMillis() >= LockOrderInfo.lockTime()) {
					orderids.add(orderid);
				}
			}

			for (int j = 0; j < orderids.size(); j++) {
				haveunlock = true;
				String orderid = orderids.get(j);
				odmap.remove(orderid);
			}
		}

		if (haveunlock) {
			set();
		}
		
	}
	
	public void check() {
//		if(!running()) {
//			new ActOpenHandle().updateActOpen(getPlayerCode(), acttype, OpenType.关闭);
//		}else {
			new ActOpenHandle().updateActOpen(getPlayerCode(), acttype, OpenType.开启);
//		}
	}
	
	// redis ---------------------------------------------------------

	static private String key(String pcode) {
		return String.format("PLAYER:%s:ACTIVE:%S", pcode, acttype);
	}
	
	public void set() {
		try {
			setv(key(getPlayerCode()), this);
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	static public ActiveModule_每周特惠 get(String playerCode, Boolean isNewPlayer) {
		try {
			ActiveModule_每周特惠 module = getv(key(playerCode));
			if (module == null) {

				//新用户不需要去查数据库
				if(!isNewPlayer){
					module = MySQLCacheDAO.getV(key(playerCode), ActiveModule_每周特惠.class);
					if(module != null){module.set();}//同步到redis
				}

				if (module == null) {
					AreaBean ab = AreaModule.area(playerCode);
					if(ab != null) {
						int areaid = ab.getAreaId();
						module = create(playerCode, areaid);
					}
				}
				if(module == null){return null;}
			}

			module.update();
			module.check();
			return module;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	//存储到数据库的方法
	public static void saveToDB(String playerCode) {
		try {
			String key = key(playerCode);
			if (touchBucket(key).isExists()) {
				ActiveModule_每周特惠 activeModule_每周特惠 = getv(key(playerCode));
				MySQLCacheDAO.setV(key, activeModule_每周特惠);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	//从redis移走数据的操作
	public static void removeAllFromRedis(String playerCode) {
		try {
			touchBucket(key(playerCode)).delete();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}
	// lock==========================================================================

	@Override
	public String lockKey() {
		return String.format("lock:%s", key(getPlayerCode()));
	}
}
